[FIXED] How to Listen to Vaadin component events in a JavaFX WebView? ~ JavaFixing 您所在的位置:网站首页 javafx webview [FIXED] How to Listen to Vaadin component events in a JavaFX WebView? ~ JavaFixing

[FIXED] How to Listen to Vaadin component events in a JavaFX WebView? ~ JavaFixing

#[FIXED] How to Listen to Vaadin component events in a JavaFX WebView? ~ JavaFixing| 来源: 网络整理| 查看: 265

[FIXED] How to Listen to Vaadin component events in a JavaFX WebView?

 May 12, 2022     java, javafx, vaadin, vaadin-flow, vaadin21   

Issue

I have created a form with Vaadin Flow (Vaadin version 21) with a save button. This form is then shown in a JavaFX WebView (Version 17.0.1) and now I want to listen to events in the form like save button from JavaFX, so I can update JavaFX parts of the application. I have seen examples like:

How to retrieve components from HTML webview in JavaFX

But they don't work since doc.getElementById("xxx") does not work at all and returns null. If I use the xpath method described there, I do get the button element, but then the adding the listener by calling the

((EventTarget) button).addEventListener("click", e -> doSomeAction(), false);

does not help. When I click the button the Vaadin listener works but the "javafx" listener does not get the event.

So my question is if there is a known solution for this Use Case?

Solution

You can invoke Java methods from JavaScript with upcalls. It works by assigning a JavaScript object to a Java one:

JSObject jso = (JSObject)webEngine.executeScript("window"); jso.setMember("java", new JavaObj());

Full example:

public class JavaObj { public void myMethod() { ... } } webEngine.setJavaScriptEnabled(true); // Obviously... webEngine.getLoadWorker().stateProperty().addListener((observable, oldState, newState) -> { if(newState == Worker.State.SUCCEEDED) { JSObject jso = (JSObject)webEngine.executeScript("window"); jso.setMember("java", new JavaObj()); } }); ((EventTarget) button).addEventListener("click", e -> UI.getCurrent().getPage().executeJs("java.myMethod()"), false);

More about upcalls here.

Answered By - Oliver Answer Checked By - Senaida (JavaFixing Volunteer)

Share This:    Facebook Twitter Google+ Stumble Digg This Answer collected from stackoverflow and tested by JavaFixing community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有